package ink.lovejinhu.leetcode;

import ink.lovejinhu.common.TreeNode;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author jinhu
 * created 2021-07-29 17:13
 */

public class Solution_0637 {
    public List<Double> averageOfLevels(TreeNode root) {
        ArrayList<Double> res = new ArrayList<>();
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        if (root != null) {
            treeNodes.push(root);
        }
        while (!treeNodes.isEmpty()) {
            TreeNode peek = treeNodes.peek();
            if (peek.left != null) {
                treeNodes.push(peek.left);
            }
            if (peek.right != null) {
                treeNodes.push(peek.right);
            }
            treeNodes.pop();
        }
        return res;
    }

    public void print(TreeNode root) {
        LinkedList<TreeNode> treeNodes = new LinkedList<>();
        if (root != null) {
            treeNodes.offer(root);
        }
        while (!treeNodes.isEmpty()) {
            TreeNode peek = treeNodes.peek();
            if (peek.left != null) {
                treeNodes.offer(peek.left);
            }
            if (peek.right != null) {
                treeNodes.offer(peek.right);
            }
            System.out.print(peek.val + "-- ");
            treeNodes.poll();
        }
    }

    public static void main(String[] args) {
        /**
         *      1
         *   2     3
         * 4   5     6
         7   8
         */
        TreeNode v1 = new TreeNode(1);
        TreeNode v2 = new TreeNode(2);
        TreeNode v3 = new TreeNode(3);
        TreeNode v4 = new TreeNode(4);
        TreeNode v5 = new TreeNode(5);
        TreeNode v6 = new TreeNode(6);
        TreeNode v7 = new TreeNode(7);
        TreeNode v8 = new TreeNode(8);
        v1.left = v2;
        v1.right = v3;
        v2.left = v4;
        v2.right = v5;
        v3.right = v6;
        v4.left = v7;
        v4.right = v8;
        Solution_0637 solution_0637 = new Solution_0637();
        solution_0637.print(v1);
    }
}
